import 'home_page.dart';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        brightness: Brightness.dark,
        primarySwatch: Colors.blue,
      ),
      home: new ScaffoldRoute(),
    );
  }
}

class ScaffoldRoute extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _ScaffoldRouteState();
  }
}

GlobalKey<ScaffoldState> _globalKey = new GlobalKey();

class _ScaffoldRouteState extends State<ScaffoldRoute> {
  int _tabbarSelectedIndex = 0;
  //定义一个globalKey, 由于GlobalKey要保持全局唯一性，我们使用静态变量存储
  // static GlobalKey<ScaffoldState> _globalKey = new GlobalKey();
  List<Widget> pages = List<Widget>();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    pages..add(HomePage())..add(Business());
  }

  void _tabBarItemTap(int index) {
    setState(() {
      _tabbarSelectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      key: _globalKey,
      drawer: new MyDrawer(), // 左边抽屉
      endDrawer: new MyDrawer(), // 右边抽屉
      // bottomNavigationBar: new BottomNavigationBar(
      //   // 底部导航
      //   items: <BottomNavigationBarItem>[
      //     BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('home')),
      //     BottomNavigationBarItem(
      //         icon: Icon(Icons.business), title: Text('business')),
      //     BottomNavigationBarItem(
      //         icon: Icon(Icons.school),
      //         title: Text(
      //           'school',
      //         )),
      //   ],
      //   currentIndex: _selectedIndex,
      //   fixedColor: Colors.blue,
      //   selectedFontSize: 12,
      //   unselectedFontSize: 12,
      //   backgroundColor: Colors.black,
      //   onTap: _onItemTap,
      // ),
      bottomNavigationBar: new BottomAppBar(
        // 这里是自定义的tabbar
        // color: Colors.white,
        shape: CircularNotchedRectangle(), // 底部导航栏打一个圆形的洞
        child: new Row(
          // mainAxisAlignment: MainAxisAlignment.spaceAround, //均分底部导航栏横向空间
          children: <Widget>[
            new Expanded(
              flex: 2,
              child: new FlatButton(
                onPressed: () {
                  _tabBarItemTap(0);
                },
                child: new Container(
                  padding: EdgeInsets.only(top: 5),
                  height: 49,
                  child: new Column(
                    children: <Widget>[
                      Icon(
                        Icons.home,
                        color: _tabbarSelectedIndex == 0
                            ? Colors.blue
                            : Colors.white,
                      ),
                      new Text(
                        'home',
                        style: TextStyle(
                            fontSize: 12,
                            color: _tabbarSelectedIndex == 0
                                ? Colors.blue
                                : Colors.white),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            new Expanded(
              flex: 1,
              child: new Container(
                height: 49,
              ),
            ),
            new Expanded(
              flex: 2,
              child: new FlatButton(
                  onPressed: () {
                    _tabBarItemTap(1);
                  },
                  child: new Container(
                    padding: EdgeInsets.only(top: 5),
                    height: 49,
                    child: new Column(
                      children: <Widget>[
                        new Icon(
                          Icons.business,
                          color: _tabbarSelectedIndex == 1
                              ? Colors.blue
                              : Colors.white,
                        ),
                        new Text(
                          'business',
                          style: TextStyle(
                              fontSize: 12,
                              color: _tabbarSelectedIndex == 1
                                  ? Colors.blue
                                  : Colors.white),
                        )
                      ],
                    ),
                  )),
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        child: Icon(Icons.add),
        backgroundColor: Colors.white,
        onPressed: () {},
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      body: pages[_tabbarSelectedIndex],
    );
  }
}

/*
抽屉菜单Drawer
Scaffold的drawer和endDrawer属性可以分别接受一个Widget作为页面的左、右抽屉菜单，
如果开发者提供了抽屉菜单，那么当用户手指从屏幕左/右向里滑动时便可打开抽屉菜单
*/

// 提供一个方法给homePage打开
void openDrawer() {
  _globalKey.currentState.openDrawer();
}

class MyDrawer extends StatelessWidget {
  const MyDrawer({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Drawer(
      child: MediaQuery.removePadding(
        context: context,
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            new Padding(
              padding:
                  EdgeInsets.only(top: MediaQuery.of(context).padding.top + 30),
              child: new Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  new Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 16),
                    child: new Stack(
                      alignment: Alignment.topLeft,
                      children: <Widget>[
                        new Positioned(
                          child: new ClipOval(
                            child: new SizedBox(
                              width: 80,
                              height: 80,
                              child: new Image.network(
                                // image: new AssetImage('assets/images/food05.jpeg'),
                                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563208213322&di=2b32bb11165dde2fe7334767ab7aaab1&imgtype=0&src=http%3A%2F%2Fc4.haibao.cn%2Fimg%2F600_0_100_0%2F1473652712.0005%2F87c7805c10e60e9a6db94f86d6014de8.jpg",
                                fit: BoxFit.cover,
                              ),
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                  new Expanded(
                    // 使用Expanded保证内容不截断 flex: 1
                    flex: 1,
                    child: new Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        new Text(
                          "刘亦菲",
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          textAlign: TextAlign.left,
                          style: TextStyle(
                            fontSize: 18,
                          ),
                        ),
                        new Text(
                          "刘亦菲，1987年8月25日出生于湖北省武汉市，华语影视女演员、歌手，毕业于北京电影学院2002级表演系本科班。",
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          textAlign: TextAlign.left,
                          style: TextStyle(
                              fontSize: 13, backgroundColor: Colors.black),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            new Expanded(
              child: ListView(
                children: <Widget>[
                  new ListTile(
                    leading: const Icon(Icons.add),
                    title: const Text('Add account'),
                  ),
                  new ListTile(
                    leading: const Icon(Icons.settings),
                    title: const Text('Manage accounts'),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

class Business extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Container(color: Colors.blue);
  }
}
